import 'dart:typed_data';

import 'package:bluetooth_protocols_parser_plugin/library.dart';
import 'package:bluetooth_protocols_parser_plugin/src/tools/costant.dart';
import 'package:bluetooth_protocols_plugin/library.dart';
import 'package:flutter/material.dart';

import '../tools/utils.dart';

// Lorin助手类
class LorinHelper {
  // 综合实时信息前缀
  static String _integratedRealtimeInfoAllPrefix = 'AAAA1663210287';

  // 获取设备的所有版本信息
  static List<Map<String, dynamic>> getVersions(Uint8List bytes) {
    // uint8_t device_id;  设备号
    // uint32_t app_version; 应用程序版本号
    // uint32_t boot_version;  引导程序版本号
    List<Map<String, dynamic>> versions = [];
    for (int i = 0; i < bytes.length; i += LORIN_ONCE_FIRMWARE_VERSION_LENGTH) {
      if (i + LORIN_ONCE_FIRMWARE_VERSION_LENGTH - 1 < bytes.length) {
        Uint8List data = bytes;
        Map<String, dynamic> version = {
          'device_id': bytes[i],
          // 将app_version四个字节，如[1, 1, 2, 3],组成版本号3.3.256，组成app_version
          'app_version':
              "${data[i + 4]}.${data[i + 3]}.${data[i + 2] * 255 + data[i + 1]}",
          'boot_version':
              "${data[i + 8]}.${data[i + 7]}.${data[i + 6] * 255 + data[i + 5]}",
        };
        versions.add(version);
      }
    }
    return versions;
  }

  //
  // bytes转换为LorinMessage
  static LorinMessage? bytesToLorinMessage(List<int> bytes) {
    // 处理主动响应数据，根据实际协议解析
    if (bytes.isEmpty) return null;
    int index = 0;
    List<LorinMessage> lorinMessages = [];
    Uint8List data = Uint8List.fromList(bytes);
    while (index < bytes.length) {
      try {
        LorinMessage msg = LorinMessage.fromList(data.sublist(index));
        lorinMessages.add(msg);
        index += msg.totalLength();
      } catch (e) {
        debugPrint(
            '数据包 (0x${Utils.uint8List2HexString(Uint8List.fromList(bytes))}) 异常: $e');
        break;
      }
    }
    // 暂时不考虑组包的上报，所以只取第一个数据包
    if (lorinMessages.isEmpty) return null;
    LorinMessage msg = lorinMessages.first;
    final String msgHexString = Utils.uint8List2HexString(msg.toBytes());
    if (msgHexString.startsWith(_integratedRealtimeInfoAllPrefix) &&
        lorinMessages.first.data != null) {
      debugPrint('🟦 数据上报: 0x${msgHexString.toLowerCase()}');
      // 以 AAAA1659210284开头的为实时数据
      return msg;
    } else {
      return null;
    }
  }

  // 电机自适应状态
  static MotorAutoStatus getMotorAutoStatus(Uint8List rsp) {
    if (rsp.length < 2) {
      return MotorAutoStatus.unknown;
    }
    // 因为是正反转，所以需要取index=0、index=1两个字节进行判断
    if (rsp.length >= 2) {
      if (rsp[0] == 0 && rsp[1] == 0) {
        return MotorAutoStatus.idle;
      } else if (rsp[0] == 1 || rsp[0] == 2 || rsp[1] == 1 || rsp[1] == 2) {
        return MotorAutoStatus.wait;
      } else if (rsp[0] == 3 && rsp[1] == 3) {
        return MotorAutoStatus.completed;
      } else if (rsp[0] == 4 || rsp[1] == 4) {
        return MotorAutoStatus.failed;
      } else if (rsp[0] == 5 || rsp[1] == 5) {
        return MotorAutoStatus.timeout;
      } else {
        return MotorAutoStatus.unknown;
      }
    } else {
      return MotorAutoStatus.unknown;
    }
  }
}
